package test_package;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

import java.util.concurrent.TimeUnit;

/**
 * Created by IIGalytska on 26.03.14.
 */
public class SignInTestSteps {
    WebDriver driver;


    @BeforeSuite(alwaysRun = true)
    public void setWebBrowser ()throws InterruptedException {
        driver = new FirefoxDriver(); //set test webbrowser
    }
    @AfterSuite
    public void shutDownSelenium() {

        driver.quit();
    }


    public void OpenPage(String url)  {


        //WebDriver driver = new FirefoxDriver()       ;
        driver.get(url)   ;
        //implicit waits

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        // get the current window handle
        String parentHandle = driver.getWindowHandle();
        //Assert that the page was opened

        Assert.assertEquals(driver.getCurrentUrl(), url);
    }

    public void GoToRegPage(String testlink){
        //explicit wait for REZETKA link
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(testlink)));
        driver.findElement(By.linkText(testlink)).click();

        // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
        }

        driver.findElement(By.partialLinkText("Вход")).click();

        WebDriverWait wait2 = new WebDriverWait(driver, 10);
        wait2.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Зарегистрироваться")));
        driver.findElement(By.linkText("Зарегистрироваться")).click();

        //Explicit Waits for registration page to be loaded
        WebDriverWait wait3 = new WebDriverWait(driver, 10);
        wait3.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[1]/div/div/div[2]/div/div/div[2]/div/form/div[6]/button")));

     }

    public void Register(String login, String email, String password){
        WebElement inputElement = driver.findElement(By.name("title"));
        inputElement.clear();
        inputElement.sendKeys(login);
        //Thread.sleep(3000);
        //here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
        Assert.assertEquals(login, inputElement.getAttribute("value"));

        WebElement inputElement2 = driver.findElement(By.name("email"));
        inputElement2.clear();
        inputElement2.sendKeys(email);
        //Thread.sleep(3000);
        //here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
        Assert.assertEquals(email, inputElement2.getAttribute("value"));

        WebElement inputElement3 = driver.findElement(By.name("password"));
        inputElement3.clear();
        inputElement3.sendKeys(password);
        //Thread.sleep(3000);
        //here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
        Assert.assertEquals(password, inputElement3.getAttribute("value"));
        //Thread.sleep(3000);

        driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div/div/div[2]/div/form/div[6]/button")).click();
        //Thread.sleep(3000);
    }

    public void VerifyExpctResult(String verification_link ){

        WebDriverWait wait4 = new WebDriverWait(driver, 50);
        wait4.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(verification_link)));
        WebElement personalPage = driver.findElement(By.linkText(verification_link));
        Assert.assertEquals(true, personalPage.isDisplayed());
        //Thread.sleep(3000);

    }

}
